package jnpf.controller;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonProperty;
import jnpf.base.entity.DictionaryDataEntity;
import jnpf.base.service.BillRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.service.DictionaryDataService;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.base.UserInfo;
import jnpf.base.vo.DownloadVO;
import jnpf.config.ConfigValueUtil;
import jnpf.exception.DataException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import jnpf.base.entity.ProvinceEntity;
import jnpf.model.whu_shock_event.*;
import jnpf.model.whu_shock_event.Whu_shock_eventPagination;
import jnpf.model.whu_shock_event.Whu_shock_eventCrForm;
import jnpf.model.whu_shock_event.Whu_shock_eventInfoVO;
import jnpf.model.whu_shock_event.Whu_shock_eventListVO;
import jnpf.model.whu_shock_event.Whu_shock_eventUpForm;
import jnpf.entity.*;
import jnpf.base.service.ProvinceService;
import jnpf.util.*;
import jnpf.base.util.*;
import jnpf.base.vo.ListVO;
import jnpf.util.context.SpringContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jnpf.entity.Whu_shock_eventEntity;
import jnpf.service.Whu_shock_eventService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.sql.Timestamp;


/**
 *
 * whu_shock_event
 * @版本： V3.2.0
 * @版权： 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2024-09-26
 */
@Slf4j
@RestController
@Api(tags = "whu_shock_event" , value = "example")
@RequestMapping("/api/example/Whu_shock_event")
public class Whu_shock_eventController {


    @Autowired
    private DynDicUtil dynDicUtil;
    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;
    @Autowired
    private UserProvider userProvider;

    @Autowired
    private Whu_shock_eventService whu_shock_eventService;
    @Autowired
    private ConfigValueUtil configValueUtil;

    @Autowired
    private DictionaryDataService dictionaryDataService;

    /**
     * 列表
     *
     * @param whu_shock_eventPagination
     * @return
     */
	@PostMapping("/getList")
    public ActionResult list(@RequestBody Whu_shock_eventPagination whu_shock_eventPagination)throws IOException{
        List<Whu_shock_eventEntity> list= whu_shock_eventService.getList(whu_shock_eventPagination);
        //处理id字段转名称，若无需转或者为空可删除
    for(Whu_shock_eventEntity entity:list){
                    entity.setType(dynDicUtil.getDicName(entity.getType()));
                    entity.setAisle(dynDicUtil.getDicName(entity.getAisle()));
                    entity.setState(dynDicUtil.getDicName(entity.getState()));
    }
	List<Whu_shock_eventListVO> listVO=JsonUtil.getJsonToList(list,Whu_shock_eventListVO.class);
    for(Whu_shock_eventListVO whu_shock_eventVO:listVO){
            }

        PageListVO vo=new PageListVO();
        vo.setList(listVO);
        PaginationVO page=JsonUtil.getJsonToBean(whu_shock_eventPagination,PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }

    @GetMapping("/totalList")
    public ActionResult List() throws IOException{
        QueryWrapper<Whu_shock_eventEntity> queryWrapper = new QueryWrapper<>();
        //未确认状态查询
        queryWrapper.lambda().eq(Whu_shock_eventEntity::getState,"f5f6019b6a5e4f9c9e03dbc9ebd5de22");
        String start_time = sdf.format(new Date());
        Date end_date = new Date();
        end_date.setDate(end_date.getDate()+1);
        String end_time = sdf.format(end_date);

        queryWrapper.lambda().between(Whu_shock_eventEntity::getTime,start_time,end_time);

        int list= whu_shock_eventService.count(queryWrapper);


        return ActionResult.success(list);
    }

    @GetMapping("/yscgList")
    public ActionResult yscgList() throws IOException{
        QueryWrapper<Whu_shock_eventEntity> queryWrapper = new QueryWrapper<>();
        //未确认状态查询
        queryWrapper.lambda().eq(Whu_shock_eventEntity::getState,"f5f6019b6a5e4f9c9e03dbc9ebd5de22");
        String start_time = sdf.format(new Date());
        Date end_date = new Date();
        end_date.setDate(end_date.getDate()+1);
        String end_time = sdf.format(end_date);

        queryWrapper.lambda().between(Whu_shock_eventEntity::getTime,start_time,end_time);
        //排序
        queryWrapper.lambda().orderByDesc(Whu_shock_eventEntity::getTime);


        List<Whu_shock_eventEntity> list= whu_shock_eventService.list(queryWrapper);
        list.forEach(l->{
            if(StringUtil.isNotEmpty(l.getType())){
                l.setType(dynDicUtil.getDicName(l.getType()));
            }
        });
        List<Whu_shock_eventListVO> listVO=JsonUtil.getJsonToList(list,Whu_shock_eventListVO.class);
        return ActionResult.success(listVO);
    }

    @GetMapping("/ok/{id}")
    public ActionResult ok(@PathVariable("id") String id) throws IOException{
        Whu_shock_eventEntity one = whu_shock_eventService.getInfo(id);
        one.setState("28c6b86397854396af54da047816b2d3");
        whu_shock_eventService.update(id,one);
        return ActionResult.success("更新成功");
    }

    @GetMapping("/no/{id}")
    public ActionResult no(@PathVariable("id") String id) throws IOException{
        Whu_shock_eventEntity one = whu_shock_eventService.getInfo(id);
        one.setState("5b2b44fc9d984da9b6d7cb40eded5d2a");
        whu_shock_eventService.update(id,one);
        return ActionResult.success("更新成功");
    }

    @GetMapping("/re/{id}")
    public ActionResult re(@PathVariable("id") String id) throws IOException{
        Whu_shock_eventEntity one = whu_shock_eventService.getInfo(id);
        one.setState("f5f6019b6a5e4f9c9e03dbc9ebd5de22");
        whu_shock_eventService.update(id,one);
        return ActionResult.success("更新成功");
    }

    @GetMapping("/yscgjcList")
    public ActionResult yscgjcList() throws IOException{
        QueryWrapper<Whu_shock_eventEntity> queryWrapper = new QueryWrapper<>();
        //未确认状态查询
        queryWrapper.lambda().eq(Whu_shock_eventEntity::getState,"f5f6019b6a5e4f9c9e03dbc9ebd5de22");
        String start_time = sdf.format(new Date());
        Date end_date = new Date();
        end_date.setDate(end_date.getDate()+1);
        String end_time = sdf.format(end_date);

        queryWrapper.lambda().between(Whu_shock_eventEntity::getTime,start_time,end_time);
        //排序
        queryWrapper.lambda().orderByDesc(Whu_shock_eventEntity::getTime);

        List<Whu_shock_eventEntity> list= whu_shock_eventService.list(queryWrapper);
        list.forEach(l->{
            if(StringUtil.isNotEmpty(l.getType())){
                l.setType(dynDicUtil.getDicName(l.getType()));
            }
        });
        List<Whu_shock_eventListVO> listVO=JsonUtil.getJsonToList(list,Whu_shock_eventListVO.class);
        return ActionResult.success(listVO);
    }

    static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");



    @GetMapping("/qrcgList")
    public ActionResult qrcgList() throws IOException{
        QueryWrapper<Whu_shock_eventEntity> queryWrapper = new QueryWrapper<>();
        //确认状态查询
        queryWrapper.lambda().eq(Whu_shock_eventEntity::getState,"28c6b86397854396af54da047816b2d3");

        String start_time = sdf.format(new Date());
        Date end_date = new Date();
        end_date.setDate(end_date.getDate()+1);
        String end_time = sdf.format(end_date);

        queryWrapper.lambda().between(Whu_shock_eventEntity::getTime,start_time,end_time);
        //排序
        queryWrapper.lambda().orderByDesc(Whu_shock_eventEntity::getTime);

        List<Whu_shock_eventEntity> list= whu_shock_eventService.list(queryWrapper);
        list.forEach(l->{
            if(StringUtil.isNotEmpty(l.getType())){
                l.setType(dynDicUtil.getDicName(l.getType()));
                l.setAisle(dynDicUtil.getDicName(l.getAisle()));
                l.setState(dynDicUtil.getDicName(l.getState()));
            }
        });
        List<Whu_shock_eventListVO> listVO=JsonUtil.getJsonToList(list,Whu_shock_eventListVO.class);
        return ActionResult.success(listVO);
    }

    @GetMapping("/greyList")
    public ActionResult greyList() throws IOException{
        QueryWrapper<Whu_shock_eventEntity> queryWrapper = new QueryWrapper<>();
        //确认状态查询
        queryWrapper.lambda().eq(Whu_shock_eventEntity::getState,"28c6b86397854396af54da047816b2d3");
        //排序
        queryWrapper.lambda().orderByDesc(Whu_shock_eventEntity::getTime);

        List<Whu_shock_eventEntity> list= whu_shock_eventService.list(queryWrapper);
        list.forEach(l->{
            if(StringUtil.isNotEmpty(l.getType())){
                l.setType(dynDicUtil.getDicName(l.getType()));
                l.setAisle(dynDicUtil.getDicName(l.getAisle()));
                l.setState(dynDicUtil.getDicName(l.getState()));
            }
        });
        List<Whu_shock_eventListVO> listVO=JsonUtil.getJsonToList(list,Whu_shock_eventListVO.class);
        return ActionResult.success(listVO);
    }
    /**
     * 创建
     *
     * @param whu_shock_eventCrForm
     * @return
     */
    @PostMapping
    @Transactional
    public ActionResult create(@RequestBody @Valid Whu_shock_eventCrForm whu_shock_eventCrForm) throws DataException {
        String mainId =RandomUtil.uuId();
        UserInfo userInfo=userProvider.get();
        Whu_shock_eventEntity entity = JsonUtil.getJsonToBean(whu_shock_eventCrForm, Whu_shock_eventEntity.class);
				entity.setId(mainId);
                whu_shock_eventService.save(entity);


        return ActionResult.success("创建成功");
}




    /**
    * 信息
    *
    * @param id
    * @return
    */
    @GetMapping("/{id}")
    public ActionResult<Whu_shock_eventInfoVO> info(@PathVariable("id") String id){
        Whu_shock_eventEntity entity= whu_shock_eventService.getInfo(id);
        Whu_shock_eventInfoVO vo=JsonUtil.getJsonToBean(entity, Whu_shock_eventInfoVO.class);

        return ActionResult.success(vo);
    }





   /**
    * 更新
    *
    * @param id
    * @return
    */
    @PutMapping("/{id}")
    @Transactional
    public ActionResult update(@PathVariable("id") String id,@RequestBody @Valid Whu_shock_eventUpForm whu_shock_eventUpForm) throws DataException {
        UserInfo userInfo=userProvider.get();
        Whu_shock_eventEntity entity= whu_shock_eventService.getInfo(id);
        if(entity!=null){
            Whu_shock_eventEntity subentity=JsonUtil.getJsonToBean(whu_shock_eventUpForm, Whu_shock_eventEntity.class);
            whu_shock_eventService.update(id, subentity);
        return ActionResult.success("更新成功");
        }else{
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

   /**
    * 删除
    *
    * @param id
    * @return
    */
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id){
        Whu_shock_eventEntity entity= whu_shock_eventService.getInfo(id);
        if(entity!=null){
            whu_shock_eventService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }


    @PostMapping("/saveShockEventApi")
    public ActionResult saveShockEventApi(
            @RequestPart("Fcode") String code,@RequestPart("Fnumber") String number,@RequestPart("Ftype") String type,
            @RequestPart("Ftime") String time,@RequestPart("Faisle") String aisle,
            @RequestPart("Fpicture") MultipartFile picture,@RequestPart("Fvideo") MultipartFile video) {


        try {


            String typeDictType="b502a49be44a417d8731ac82cac55e08";
            String aisleDictType="c6acff5057ca4a1dac89a00748320f30";

            Whu_shock_eventEntity entity=new Whu_shock_eventEntity();
            entity.setCreatortime(new Date());
            entity.setCreatoruserid("admin");


            //设备号
            entity.setCode(code);
            //车牌号
            entity.setNumber(number);
            //事件时间
            if(StringUtils.isNotBlank(time)){
                entity.setTime(new Date());
            }
            entity.setState("f5f6019b6a5e4f9c9e03dbc9ebd5de22");

            //车辆类型
            DictionaryDataEntity typeDict=dictionaryDataService.getOne(new QueryWrapper<DictionaryDataEntity>().eq("F_DictionaryTypeId",typeDictType).eq("F_FullName",type));
            if(typeDict!=null){
                entity.setType(typeDict.getId());
            }
            //冲岗通道
            DictionaryDataEntity aisleDict=dictionaryDataService.getOne(new QueryWrapper<DictionaryDataEntity>().eq("F_DictionaryTypeId",aisleDictType).eq("F_FullName",aisle));
            if(aisleDict!=null){
                entity.setAisle(aisleDict.getId());
            }



            File fp=new File(configValueUtil.getWebAnnexFilePath());
            //判断文件是否存在，不存在则创建文件
            if(!fp.exists()){
                fp.mkdirs();
            }

            //保存图片
            if (!picture.isEmpty()) {
                String pictureName=System.currentTimeMillis()+"_"+picture.getOriginalFilename();
                String filePath = configValueUtil.getWebAnnexFilePath() + pictureName;
                picture.transferTo(new File(filePath));

                entity.setPicture("/WebAnnexFile/"+pictureName);
            }

            //保存视频
            if (!video.isEmpty()) {
                String videoName=System.currentTimeMillis()+"_"+video.getOriginalFilename();
                String filePath = configValueUtil.getWebAnnexFilePath() + videoName;
                video.transferTo(new File(filePath));

                entity.setVideo("/WebAnnexFile/"+videoName);
            }


            this.whu_shock_eventService.save(entity);



            return ActionResult.success("创建成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail("创建失败");
        }
    }


}
